package com.example.demo.controller;

import com.example.demo.pojo.Medicine;
import com.example.demo.pojo.Prescribe;
import com.example.demo.service.MedicineService;
import com.example.demo.service.PrescribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller("prescribeController")
@RequestMapping("/prescribe")
public class PrescribeControlller {

    @Autowired
    private PrescribeService prescribeService;

    @Autowired
    private MedicineService medicineService;

    @RequestMapping("/toprescribe")
    public String toPrescribe() {
        return "prescribe";
    }

    @RequestMapping("/showprescribe")
    @ResponseBody
    public List<Prescribe> showPrescribe() {
        return prescribeService.showAll();
    }

    @PostMapping("/addprescribe")
    @ResponseBody
    public int addPrescribe(@RequestBody Prescribe prescribe) {
        int flag = prescribeService.addPrescribe(prescribe);
        return flag;
    }

    @RequestMapping("/searchprescribe")
    @ResponseBody
    public List<Prescribe> searchprescribe(String case_id) {
        int id = Integer.valueOf(case_id).intValue();
        return prescribeService.searchprescribe(id);
    }

    @PostMapping("/searchmedicine")
    @ResponseBody
    public List<Medicine> searchMedicine(@RequestBody Prescribe prescribe) { ;
        return medicineService.searchMedicine(prescribe.getMedicine_name());
    }
}
